import Vue from 'vue';

export function checkFormValue(formData) {
  let data = {};
  let validationFailed = false;
  let msgPrefix = '请选择';

  for(let key in formData){
    msgPrefix = '请选择';
    if (formData[key].type === 'text' || formData[key].type === 'input' || formData[key].type === 'textarea' || formData[key].type === 'number' || typeof(formData[key].type) === 'undefined') {
      msgPrefix = '请输入';
    }

    if(formData[key].required && typeof formData[key].value !== 'number' && (!formData[key].value || formData[key].value.length==0)){
      validationFailed = true;
      formData[key].customError = `${msgPrefix}${formData[key].label}`;
    }

    if (formData[key].rules && formData[key].rules.length > 0) {
      for (const rule of formData[key].rules) {
        // 举例，各类型检查，可以统一写在此处
        if([''].includes(formData[key].value)){
        }else if (rule.type === 'email') {
          const emailRegex = /^\S+@\S+\.\S+/;

          if (!emailRegex.test(formData[key].value)) {
            if (typeof rule.when === 'function') {
              // 也支持传入函数验证
              const ruleWhenResult = rule.when(formData);

              if (ruleWhenResult) {
                validationFailed = true;
                formData[key].customError = rule.message || `校验错误，${msgPrefix}${formData[key].label}`;
              }
            }

            validationFailed = true;
            formData[key].customError = `信息规格错误，${msgPrefix}${formData[key].label}`;
          }
        }else if (rule.type === 'phone') {
          const emailRegex = /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/;

          if (!emailRegex.test(formData[key].value)) {
            if (typeof rule.when === 'function') {
              // 也支持传入函数验证
              const ruleWhenResult = rule.when(formData);

              if (ruleWhenResult) {
                validationFailed = true;
                formData[key].customError = rule.message || `校验错误，${msgPrefix}${formData[key].label}`;
              }
            }

            validationFailed = true;
            formData[key].customError = '请输入正确的手机号或座机号';
          }
        } else if(rule.type === 'insureNo'){
          const emailRegex = /^[A-Za-z0-9]+$/
          // 英文加数字，长度为9-50
          if (!emailRegex.test(formData[key].value) || (formData[key].value.length < 9 || formData[key].value.length > 50)) {
            validationFailed = true;
            formData[key].customError = rule.message || `校验错误，${msgPrefix}${formData[key].label}`;
          }
        } else {
          if (typeof rule.when === 'function') {
            // 也支持传入函数验证
            const ruleWhenResult = rule.when(formData);

            if (ruleWhenResult) {
              validationFailed = true;
              formData[key].customError = rule.message || `校验错误，${msgPrefix}${formData[key].label}`;
            }
          }
        }

        // 其他错误类型检查参考以上
      }
    }

    if(typeof formData[key].value === 'boolean'){
      data[key] = Number(formData[key].value);
    }else {
      data[key] = formData[key].value;
    }
  }

  return validationFailed ? false : data;
}

export function asyncSelectListener(formData, changeData) {
  let fieldsArray = [];

  for (let key in formData) {
    if (formData[key].type === 'async-select') {
      fieldsArray.push(key);
    }
  }

  fieldsArray.forEach(field => {
    if (changeData && changeData.prop === field) {
      console.log(`set ${field} value to:`, changeData.value);
      formData[field].value = changeData.value || '';
    }
  });
}
